package com.ruoyi.blog.web;

import com.ruoyi.blog.pojo.AuthUser;
import com.ruoyi.blog.service.UserService;
import com.ruoyi.blog.util.IdWorker;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;
import java.util.regex.Pattern;

@Controller
@RequestMapping("page")
public class RegController {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private UserService userService;

    /**
     * 注册页
     * @return
     */
    @GetMapping("/reg")
    public String pageReg() {
        return "user/reg";
    }

    /**
     * 用户注册
     * @param authUser
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/user-register")
    public String addUser(AuthUser authUser, Map<String, Object> map, HttpSession session) {
        String username = authUser.getUsername();
        String password = authUser.getPassword();
        // String email = authUser.getEmail();
        String mobile = authUser.getMobile();
        QueryWrapper<AuthUser> queryWrapper = new QueryWrapper<AuthUser>();
        queryWrapper.eq("username", username);
        AuthUser user = userService.getOne(queryWrapper);
        if (StringUtils.isBlank(username)) {
            map.put("username", "用户名不能为空！");
            return "user/reg";
        }
        if (user != null) {
            map.put("username", "用户名已存在！");
            return "user/reg";
        }
        if (StringUtils.isBlank(password)) {
            map.put("password", "密码不能为空！");
            return "user/reg";
        }
        // if (StringUtils.isBlank(email)) {
        //     map.put("email", "邮箱不能为空！");
        //     return "user/reg";
        // }
        String checkMobile = "^((\\+|00)86)?((134\\d{4})|((13[0-3|5-9]|14[1|5-9]|15[0-9]|16[2|5|6|7]|17[0-8]|18[0-9]|19[0-2|5-9])\\d{8}))$";
        if (StringUtils.isBlank(mobile)) {
            map.put("msgMobile", "手机号不能为空！");
            return "user/reg";
        }
        if (!Pattern.matches(checkMobile, mobile)) {
            map.put("msgMobile", "请填写正确的手机号！");
            return "user/reg";
        }
        authUser.setId(idWorker.nextId() + "");
        // 对密码加密
        String hashpwd = BCrypt.hashpw(password, BCrypt.gensalt());
        authUser.setPassword(hashpwd);
        authUser.setUsername(username);
        authUser.setMobile(mobile);
        authUser.setStatus("y");
        authUser.setCreateDate(LocalDateTime.now());
        authUser.setUpdateDate(LocalDateTime.now());
        session.setAttribute("success", "注册成功！");
        userService.save(authUser);
        return "redirect:/page/login";
    }
}
